package cn.shuo.app.controller;

import cn.niter.forum.provider.BaiduCloudProvider;
import cn.shuo.app.db.ext.ACommentExtMapper;
import cn.shuo.app.db.ext.AQuestionExtMapper;
import cn.shuo.app.db.ext.BlockExtMapper;
import cn.shuo.app.db.mapper.AadMapper;
import cn.shuo.app.db.mapper.AdTypeMapper;
import cn.shuo.app.db.model.*;
import cn.shuo.app.model.*;
import cn.shuo.app.service.AQuestionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("app/api")
@RestController
public class AIndexController {

    @Autowired
    private ACommentExtMapper aCommentExtMapper;
    @Autowired
    private BaiduCloudProvider baiduCloudProvider;
    @Autowired
    private AQuestionService AQuestionService;
    @Autowired
    private BlockExtMapper blockExtMapper;
    @Autowired
    private AQuestionExtMapper questionExtMapper;
    @Autowired
    private AadMapper aadMapper;
    @Autowired
    private AdTypeMapper adTypeMapper;


    @ResponseBody
    @GetMapping(value = "/index")
    public ResultVo index() {
        int key = 1;
        AdType adType = adTypeMapper.selectByPrimaryKey(key);
        Aad aad = new Aad();
        aad.setTypeId(key);
        List<Aad> aads = aadMapper.list(aad);
        aads.stream().forEach(e->{
            e.setUrl("../static/temp/1.jpg");
        });
        ADsDto aDsDto = new ADsDto();
        BeanUtils.copyProperties(adType,aDsDto);
        aDsDto.setAditems(aads);
        AindexDto aindexDto = new AindexDto(aDsDto);
        //加载导航栏，广告等静态数据
        return ResultVo.ok(aindexDto);
    }


    /**
     * 小说分类的导航数据
     * @param type 1。男频 2，女频 3，本站
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/block", method = RequestMethod.GET)
    public ResultVo board(int type) {
        //加载前10条
        //问题的创建者的姓名+头像
        AQuestionDto.UserBean user ;//
        //问题的头像信息
        APhotoAlbum photoAlbum;
        ABlock block;//
        PageHelper.startPage(0,10);
        ABlock aBlock = new ABlock();
        aBlock.setType(type);
        List<ABlock> blocks = blockExtMapper.list(aBlock);
//        List<ABlock> blocks = blockExtMapper.selectList(0,10);
        return ResultVo.ok(blocks);
    }

    @ResponseBody
    @RequestMapping(value = "/posts/board-posts", method = RequestMethod.GET)
    public ResultVo board_posts(int board_id,int page_size,int page) {
        AComment comment = null;
        PageHelper.startPage(page,page_size);
        List<AQuestion> questions = questionExtMapper.selectList(board_id);
        PageHelper.startPage(page,page_size);
        List<AQuestionUser> questionUsers = questionExtMapper.selectAqUserList(board_id);
//
//        List<AComment> comments = aCommentExtMapper.selectList(1);
//        if (comments != null && !comments.isEmpty()) {
//            comment = comments.get(0);
//        }
        questionUsers.stream().forEach(item -> {
            item.setCreated_at("6小时前");
            item.setUpdated_at("2周前");
        });
        return ResultVo.ok(questionUsers);
    }




}
